package com.student.service.impl;

import com.student.dao.StudentDao;
import com.student.dao.impl.StudentDaoImpl;
import com.student.pojo.StuInfo;
import com.student.service.StudentService;
import com.student.util.DBHelper;

import java.sql.Connection;
import java.util.List;

/**
 * 学生业务逻辑层实现类
 */
public class StudentServiceImpl implements StudentService {

    Connection con = DBHelper.getCon();
    StudentDao studentDao = new StudentDaoImpl(con);

    @Override
    public Integer addStudent(StuInfo student) {
        int n=0;
        try {
            con= DBHelper.getCon();
            con.setAutoCommit(false);//关闭事务的自动提交
            n = studentDao.addStudent(student);
            con.commit();//提交
        }catch (Exception e){
            e.printStackTrace();
            if(con!=null){
                try {
                    con.rollback();//事务回滚
                }catch (Exception ex){
                    System.err.println("添加用户 StudentServiceImpl(addStudent)“事务回滚”报错!");
                    System.err.println("...Exception message:"+ex.getMessage());
                }
            }
        }finally {
            DBHelper.close(null,null,con);
        }
        return n;
    }

    @Override
    public Integer delStudent(Integer studentId) {
        int n=0;
        try {
            con= DBHelper.getCon();
            con.setAutoCommit(false);//关闭事务的自动提交
            n = studentDao.delStudent(studentId);
            con.commit();//提交
        }catch (Exception e){
            e.printStackTrace();
            if(con!=null){
                try {
                    con.rollback();//事务回滚
                }catch (Exception ex){
                    System.err.println("删除用户 StudentServiceImpl(delStudent)“事务回滚”报错!");
                    System.err.println("...Exception message:"+ex.getMessage());
                }
            }
        }finally {
            DBHelper.close(null,null,con);
        }
        return n;
    }

    @Override
    public Integer updateStudent(StuInfo student) {
        int n=0;
        try {
            con= DBHelper.getCon();
            con.setAutoCommit(false);//关闭事务的自动提交
            n = studentDao.updateStudent(student);
            con.commit();//提交
        }catch (Exception e){
            e.printStackTrace();
            if(con!=null){
                try {
                    con.rollback();//事务回滚
                }catch (Exception ex){
                    System.err.println("修改用户 StudentServiceImpl(updateStudent)“事务回滚”报错!");
                    System.err.println("...Exception message:"+e.getMessage());
                }
            }
        }finally {
            DBHelper.close(null,null,con);
        }
        return n;
    }

    @Override
    public List<StuInfo> seeStudent(Integer id, String name) {
        List<StuInfo> stuInfoList=null;
        try {
            con=DBHelper.getCon();
            stuInfoList = studentDao.seeStudent(id, name);
        }catch (Exception e){
            System.err.println("查询用户 StudentServiceImpl(seeStudent)报错!");
            System.err.println("...Exception message:"+e.getMessage());
            e.printStackTrace();
        }finally {
            DBHelper.close(null,null,con);
        }
        return stuInfoList;
    }

    @Override
    public StuInfo studentLogin(String name, String password, String role) {
        StuInfo stuInfo = null;
        try {
            con = DBHelper.getCon();
            stuInfo = studentDao.studentLogin(name, password, role);
        }catch (Exception e){
            System.err.println("用户登录 StudentServiceImpl(studentLogin)报错!");
            System.err.println("...Exception message:"+e.getMessage());
            e.printStackTrace();
        }finally {
            DBHelper.close(null,null,con);
        }
        return stuInfo;
    }
}
